package cn.dansj.common.mail;

import cn.dansj.common.mail.bean.MailConfigure;
import cn.dansj.common.mail.bean.MailContent;
import cn.dansj.common.mail.bean.MailFile;
import cn.dansj.common.mail.engine.CustomTemplateEngine;
import cn.dansj.common.mail.utils.javamail.JavaMailSenderImpl;
import cn.dansj.common.mail.utils.javamail.MimeMessageHelper;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import org.thymeleaf.TemplateEngine;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

public class MailUtils {
    private final JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
    private static final TemplateEngine templateEngine = new CustomTemplateEngine();
    private InternetAddress username;

    public MailUtils(MailConfigure mailConfigure) {
        setConfig(mailConfigure);
    }

    public static MailUtils getInstance(MailConfigure mailConfigure) {
        // set application
        return new MailUtils(mailConfigure);
    }

    public static MailUtils getInstance() {
        return getInstance(null);
    }

    public MailUtils() {
    }

    public void setConfig(MailConfigure mailConfigure) {
        try {
            if (mailConfigure == null) return;
            javaMailSender.setHost(mailConfigure.getHost());
            javaMailSender.setPort(mailConfigure.getPort());
            username = new InternetAddress(mailConfigure.getUsername());
            javaMailSender.setUsername(username.getAddress());
            javaMailSender.setPassword(mailConfigure.getPassword());
            javaMailSender.setDefaultEncoding(mailConfigure.getEncoding());

            Properties properties = new Properties();
            properties.setProperty("mail.smtp.auth", String.valueOf(mailConfigure.isAuth()));
            properties.setProperty("mail.smtp.ssl.enable", String.valueOf(mailConfigure.isSsl()));
            properties.setProperty("mail.debug", String.valueOf(mailConfigure.isDebug()));
            javaMailSender.setJavaMailProperties(properties);
        } catch (AddressException addressException) {
            throw new IllegalArgumentException(addressException);
        }
    }

    public void send(MailContent mailContent) throws UnsupportedEncodingException, MessagingException {
        final MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        helper.setSubject(mailContent.getSubject());
        helper.setFrom(username);
        helper.setTo(mailContent.getRecipient());
        helper.setSentDate(new Date());
        MailFile[] files = mailContent.getFiles();
        if (files != null) {
            for (MailFile file : files) {
                helper.addAttachment(MimeUtility.encodeWord(file.getName(), "UTF-8", "B"), file.getFile());
            }
        }
        if (mailContent.getContext() != null) {
            String process = templateEngine.process(mailContent.getTemplate(), mailContent.getContext());
            helper.setText(process, true);
        } else {
            helper.setText(mailContent.getMessage().replace("\n", "<br/>").replace(" ", "&nbsp;"), mailContent.isHtml());
        }

        javaMailSender.send(mimeMessage);
    }
}
